﻿Imports TBCC.UtilityMobile
''' <summary>
''' 修改控制器的网络地址
''' </summary>
''' <remarks></remarks>
Public Class NetAddressConfigForm

    Private _isModify As Boolean = False
    Private Const pacAddress_offset As Short = 2003

    Private Sub NetAddressConfigForm_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
        setmodifystate(False)
        Me.btnModify.Enabled = False
        Me.Timer1.Start()
    End Sub
    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click

        Me.btnModify.Enabled = False
        If EnterModify() = False Then
            'msg.Show("其它地方正在修改参数,请稍后再试.")
            Me.btnModify.Enabled = True
            Exit Sub
        End If

        SetModifyState(True)
    End Sub
    Private Function EnterModify() As Boolean

        Dim result As Boolean
        result = ParameterModifyClass.EnterModify(g_netID)

        If result = False Then
            Return False
        End If

        ''开始发送心跳包
        'Me.TimerHeart.Start()

        _isModify = True

        Return True
    End Function
    
    Private Sub SetModifyState(ByVal flag As Boolean)

        Me.nudPAC.Enabled = flag
        Me.btnModify.Enabled = Not flag
        Me.btnSave.Enabled = flag
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        '检查输入MAC地址或网络地址的有效性
        If CheckValidate() = False Then
            Exit Sub
        End If

        Me.Cursor = Cursors.WaitCursor

        Dim result As Boolean
       
        Me.btnSave.Enabled = False
        Me.nudPAC.Enabled = False

        result = Save()

        If result = True Then
            LoadData(Nothing)
            msg.Show("修改成功")
        Else
            msg.ShowError("修改失败")
        End If

        Me.Cursor = Cursors.Default

         setmodifystate(False)

    End Sub

    ''' <summary>
    ''' 检查输入数据的有效性
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValidate() As Boolean
       

        Return True
    End Function

    ''' <summary>
    ''' 修改网络地址
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        Dim result As Boolean

        Dim value As Short
        value = Me.nudPAC.Value
        result = Save(pacAddress_offset, value)

        If result = False Then
            Return False
        End If
        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(g_netID) = False Then
            Return False
        End If

        return True 
    End Function
    Private Function save(ByVal index As UShort, ByVal value As Short) As Boolean
        Dim offset As UShort = index + 1000
        'Dim offset As UShort = index
        Return COMHelper.Instance.Write(g_netID, FunctionCode.WordWrite, offset, value)
    End Function

   

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        sender.Stop()
        LoadData(Nothing)
    End Sub
    Private Sub SetModify(ByVal flag As Boolean)
        Me.btnModify.Enabled = flag
    End Sub
    Private Sub LoadData(ByVal ar As IAsyncResult)
        Dim buf() As UShort
        
        buf = COMHelper.Instance.Send4UShort(g_netID, FunctionCode.WordWritableRead, pacAddress_offset, 1)



        If buf Is Nothing Then
            Me.Invoke(New Action(Of Boolean)(AddressOf SetModify), False)
            msg.ShowError(g_strReadError)
            Exit Sub
        End If

        Me.Invoke(New Action(Of Boolean)(AddressOf SetModify), True)
        Me.nudPAC.Value = buf(0)
        
    End Sub
End Class